package com.test2;

import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;

import cn.hutool.core.lang.Snowflake;

public class Test {
	
	public static void main(String[] args) {
		Set<String> ss = new HashSet<>();
//		SnowflakeIdWorker snow = new SnowflakeIdWorker(Test.getWorkId(),Test.getDataCenterId());
//		for(int i = 0;i<10000;i++) {
//			String value = String.valueOf(snow.nextId());
//			System.err.println(value);
//			ss.add(value);
//		}
//		System.err.println(ss.size());
		Date time = new Date(1671074276000L);//1671074276000L
		Snowflake snow = new Snowflake(time,Test.getWorkId(),Test.getDataCenterId(),true);
			for(int i = 0;i<1000;i++) {
			String value = String.valueOf(snow.nextId());
//			System.err.println(value);
			ss.add(value);
		}
		System.err.println(ss.size());
	}
	
	
    /**
     * workId使用IP生成
     * @return workId
     */
    private static Long getWorkId() {
        try {
            String hostAddress = Inet4Address.getLocalHost().getHostAddress();
            System.out.println(hostAddress);
            int[] ints = StringUtils.toCodePoints(hostAddress);
            int sums = 0;
            for (int b : ints) {
                sums = sums + b;
            }
            return (long) (sums % 32);
        }
        catch (UnknownHostException e) {
            // 失败就随机
            return RandomUtils.nextLong(0, 31);
        }
    }


    /**
     * dataCenterId使用hostName生成
     * @return dataCenterId
     */
    private static Long getDataCenterId() {
        try {
            String hostName = SystemUtils.getHostName();
            System.out.println(hostName);
            int[] ints = StringUtils.toCodePoints(hostName);
            int sums = 0;
            for (int i: ints) {
                sums = sums + i;
            }
            return (long) (sums % 32);
        }
        catch (Exception e) {
            // 失败就随机
            return RandomUtils.nextLong(0, 31);
        }
    }
	

}
